tool
extends EditorScript
#  导入数据脚本

# 导入文件路径
const area_data_path = "res://areaData.json"

var data = null

func _run():
	read_data(area_data_path)
	import_dic()
	import_shop()
	import_config()
	import_map()
	import_npc()
	import_sprite()
	import_dialog()
	import_drama()
	
	import_skill()
	
	import_server()
	
	
	
	print_debug("导入成功...")
	
# 据导出
func read_data(path):
	data = FileHelper.read(path)
	

# 重置Id
func reset_id_list(list:Array):
	for item in list:
		var index = item.get("uniqueIndex", null)
		if index:
			item["id"] = index


# 导出字典
func import_dic():
	var gyms = data["dic"]["gyms"]
	reset_id_list(gyms)
	
	var consumeCoin = data["dic"]["consumeCoin"]
	reset_id_list(consumeCoin)
	
	for i in gyms:
		i["icon"] = add_assets_path(i["icon"])
	
	
	DataSource.set_data_list(DataSource.gyms, gyms)
	DataSource.set_data_list(DataSource.coin_consume, consumeCoin)



func add_assets_path(path):
	if path:
		return "res://map/"+path
	else:
		return ""


# 导出商店数据
func import_shop():
	var shops = data["item"]["shops"]
	var shopDetails = data["item"]["shopDetails"]
	
	reset_id_list(shops)
	
	reset_id_list(shopDetails)
	
	DataSource.set_data_list(DataSource.shop, shops)
	DataSource.set_data_list(DataSource.shop_details, shopDetails)
	
	

# 导出配置
func import_config():	
	var conditions = data["config"]["condition"]
	reset_id_list(conditions)
	
	DataSource.set_data_list(DataSource.conditions, conditions)
	
		
	
	
func print_arr(list, prefix=""):
	if list:
		print(prefix, "[")
		for i in list:
			if i is Dictionary:
				i = i.duplicate(true)
				if i.has("uniqueIndex"):
					i.erase("uniqueIndex")
				
				if i.has("areaId"):
					i.erase("areaId")
				
				print("\t", JSON.print(i), ",")
			else:
				print("\t", i, ",")
		print("]")
	else:
		print(prefix, "[]")

func import_dialog():
	var dialigs = data["dialog"]["dialogs"]
	reset_id_list(dialigs)
	
	for dialog in dialigs:
		server_dilag_to_engine(dialog)
		
	
	
	DataSource.set_data_list(DataSource.dialig, dialigs)
	


func server_dilag_to_engine(dialog):
	var json = dialog["data"]
	dialog["type"] = json["type"]
	
	if dialog["type"] == 0:
		var list = []
		
		var contents = json["contents"]
		
		var paragraphs = json["paragraph"]
		var select_paragraphs = json["select_paragraph"]
		
		for i in range(contents.size()):
			var content = contents[i]
			var index = str(i+1)
			var paragraph = paragraphs.get(index, null)
			var select_paragraph = []
			
			if select_paragraphs.get(index, null):
				if !select_paragraphs[index].get("select", null):
					print(dialog)
				
				var select_list = select_paragraphs[index]["select"]
				
				for j in range(select_list.size()):
					var parage = {
						"title": select_list[j],
						"paragraph": select_paragraphs[index].get(str(j+1), null)
					}
					select_paragraph.append(parage)
					
			var data = {
				"content": content,
				"paragraph": paragraph,
				"select_paragraph": select_paragraph,
			}
			
			list.append(data)
			
			dialog["contents"] = list
	
	else:
		dialog["contents"] = dialog["data"]["dialogs"]
		

func import_map():
	# 地图注册
	var map_registers = data["map"]["register"]
	reset_id_list(map_registers)
	
	var fight_map_correlation = data["server"]["poke_game_data_npc_fight_map_correlation"]
	reset_id_list(fight_map_correlation)
	
	for map_register in map_registers:
		for correlation in fight_map_correlation:
			if map_register["id"] == correlation["mapId"]:
				map_register["correlation"] = correlation["correlationMapId"]
		
		if map_register.get("path"):
			map_register["path"] = map_register["path"].replace("mod/areas/hoenn", "map/")
			
	
	
	var trees = data["map"]["tree"]
	reset_id_list(trees)
	DataSource.set_data_list(DataSource.map_trees, trees)
	
	var pokemonCenterRecovers = data["map"]["pokemonCenterRecovers"]
	reset_id_list(pokemonCenterRecovers)
	
	for center in pokemonCenterRecovers:
		center["mapId"] = center["id"]
		
	DataSource.set_data_list(DataSource.map_center_recover, pokemonCenterRecovers)
	
	# 装饰物
	var decorations = data["map"]["decorations"]
	reset_id_list(decorations)
	
	for i in decorations:
		i["res"] = add_assets_path(i["res"])
	
	DataSource.set_data_list(DataSource.map_decoration,decorations)
	
	
	DataSource.set_data_list(DataSource.map_register, map_registers)
	
	var doors = data["map"]["door"]
	reset_id_list(doors)
	
	for i in doors:
		i["res"] = add_assets_path(i["res"])
		
	DataSource.set_data_list(DataSource.map_door, doors)
	
	var props = data["map"]["prop"]
	reset_id_list(props)
	
	for prop in props:
		prop["res"] = add_assets_path(prop["res"])
	
	DataSource.set_data_list(DataSource.map_prop, props)
	
	var interactives = data["map"]["interactive"]
	reset_id_list(interactives)
	
	for interactive in interactives:
		interactive["res"] = add_assets_path(interactive["res"])
		interactive["audio"] = add_assets_path(interactive["audio"])
	
	DataSource.set_data_list(DataSource.map_interactive, interactives)
	
	var npcs = data["map"]["npc"]
	
	for npc in npcs:
		npc["ipCoin"] = npc.get("ipCoin", 0)
		if npc.get("npcRes"):
			npc["npcRes"] = add_assets_path(npc["npcRes"])
	
	reset_id_list(npcs)
	
	DataSource.set_data_list(DataSource.map_npc, npcs)
	
	# 音乐区域
	var maps = data["map"]["map"]
	
	for i in maps:
		i["musicName"] = add_assets_path(i["musicName"])
	
	reset_id_list(maps)
	
	DataSource.set_data_list(DataSource.map_area, maps)
	
	
func import_npc():
	var npc_fight = data["npc"]["fight"]
	reset_id_list(npc_fight)
	
	
	var fight_pokemon = data["server"]["poke_game_data_npc_fight_pokemon"]
	reset_id_list(fight_pokemon)
	
	for fight in npc_fight:
		fight["spriteList"] = []
		for sprite in fight_pokemon:
			if fight["id"] == sprite["npcFightId"]:
				fight["spriteList"].append(sprite)
	
	
	DataSource.set_data_list(DataSource.npc_fight, npc_fight)


func import_drama():
	var cores = data["drama"]["core"]
	var mains = data["drama"]["main"]
	
	reset_id_list(mains)
	reset_id_list(cores)
	
	var ids = []
	var dramas = []
	
	for item in mains:
		var id = int(item["id"])
		if ids.find(id) == -1:
			ids.append(id)
			
	for item in cores:
		var id = int(item["id"])
		if ids.find(id) == -1:
			ids.append(id)
	
	# 剧本排序	
	ids.sort()
	
	for id in ids:
		var main = get_defult_mains(mains, id)
		var core = get_defult_cores(cores, id)
		
		main["name"] = core["name"]
		main["remark"] = core["remark"]
		
		main["type"] = core["data"].get("type", 1)
		main["drama_start_state"] = core["data"].get("drama_start_state", [])
		
		main["drama_sequence"] = core["data"].get("drama_sequence", [])
		
		main["drama_hinder"] = core["data"].get("drama_hinder", [])
		main["drama_next_normal_hinder"] = core["data"].get("drama_next_normal_hinder", [])
		
		dramas.append(main)
	
	
		
	DataSource.set_data_list(DataSource.drama, dramas)


func get_defult_mains(mains, id):
	for main in mains:
		if int(main["id"]) == id:
			return main
		
	
	return {"aRepeat":0, "backIds":"", "clearIds":"", "failBack":0, 
	"id":str(id), "interruptBackPokemonCenter":0, "interruptCompletion":0, "interruptResetRolePosition":"", 
	"next":"", "rewardItems":"", "rewardSpriteBadge":""}

func get_defult_cores(cores, id):
	for item in cores:
		if int(item["id"]) == id:
			return item
		
	return {
		"data": {
			"drama_hinder": [],
			"drama_next_ids": [],
			"drama_next_normal_hinder":[],
			"drama_sequence": [],
			"type": 1
		},
		"id": str(id),
		"name": "",
		"remark": "",
	}


func import_sprite():
	var fieldPokemonAreas = data["sprite"]["fieldPokemonAreas"]
	var fieldPokemonItems = data["sprite"]["fieldPokemonItems"]
	var init = data["sprite"]["init"]
	var attachItems = data["sprite"]["attachItems"]
	var pickPokemonChanges = data["sprite"]["pickPokemonChanges"]
	
	
	reset_id_list(fieldPokemonAreas)
	reset_id_list(fieldPokemonItems)
	reset_id_list(init)
	reset_id_list(attachItems)
	reset_id_list(pickPokemonChanges)
	
	
	var item_map = {}
	
	
	for item in attachItems:
		var id = item["pokemonId"]
		var attach_item =  item_map.get(id, {"id": id, "itemList":[]})
		
		attach_item["itemList"].append(item)
		
		item_map[id] = attach_item
		
	
	DataSource.set_data_list(DataSource.map_sprite_area, fieldPokemonAreas)
	DataSource.set_data_list(DataSource.map_sprite_area_item, fieldPokemonItems)
	DataSource.set_data_list(DataSource.poke_item, item_map.values())
	DataSource.set_data_list(DataSource.init_sprite, init)
	DataSource.set_data_list(DataSource.sprite_change, pickPokemonChanges)


# 导入技能
func import_skill():
	var teach_shop = data["skill"]["teachShop"]
	reset_id_list(teach_shop)
	
	DataSource.set_data_list(DataSource.teach_shop, teach_shop)
	
func import_server():
	var appoint_sprite = data["server"]["poke_game_data_field_appoint_pokemon"]
	var pick_items = data["server"]["poke_game_data_pick_dialog_item"]
	var pick_sprites = data["server"]["poke_game_data_pick_dialog_pokemon"]
	
	reset_id_list(pick_items)
	reset_id_list(pick_sprites)
	reset_id_list(appoint_sprite)
		

	DataSource.set_data_list(DataSource.pick_item, pick_items)
	DataSource.set_data_list(DataSource.pick_sprite, pick_sprites)
	DataSource.set_data_list(DataSource.appoint_sprite, appoint_sprite)
	


